package org.mog.album.listener;

import cn.hutool.json.JSONUtil;
import context.MQContext;
import entity.pojo.Album;
import lombok.RequiredArgsConstructor;
import org.mog.album.service.IAlbumService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class NewAlbumListener {
    private final IAlbumService albumService;

    /**
     * 监听是否有新专辑
     *
     * @param albumJson
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MQContext.ALBUM_QUEUE, durable = "true"),
            exchange = @Exchange(name = MQContext.ALBUM_EXCHANGE, type = ExchangeTypes.DIRECT),
            key = MQContext.NEW_ALBUM_ROUTING_KEY
    ))
    public void listenNewAlbum(String albumJson) {
        //新增专辑
        Album album = JSONUtil.toBean(albumJson, Album.class);
        albumService.insert(album);
    }
}
